#include "mat_ui_setting.h"
#include "mat_ui_animator.h"
#include "mat_gamerule.h"
#include "mat_ui_msgbox.h"
#include "mat_ui_helper.h"

#include "ui/ui_manager.h"
#include "gameflow/game_flow_manager.h"
#include "gameflow/ui_popup.h"
#include "gameflow/ui_state.h"
#include "application/game_application.h"
#include "application/game_rms.h"





MATUISetting::MATUISetting(gameflow::UIPopup* pPopup) : 
									MATUIPopupAnimator(pPopup),
									m_AlphaGlow(0),
									m_GlowAddVal(1)
{

	
	if(m_pPopup)
	{
		m_pAnimateWidget = m_pPopup->GetWidget("SettingBG");
		MATUIPopupAnimatorMgr::Instance().AddPopupAnimator(MATUIPopupAnimatorMgr::POPUP_NAMES_SETTING,this);
		
		m_Type = ANIMATION_SLIDEFROMRIGHT;

		m_pPopup->SetWidgetFunctor("Resume_2",UI_DELEGATE(MATUISetting, OnButtonResume));		

		m_pUIButtonGlow = m_pPopup->GetWidget("backbutton_glow");
		
		m_pButtons.push_back(new MATUIHelper(m_pPopup->GetWidget("Resume_2")));
		m_pButtons.push_back(new MATUIHelper(m_pUIButtonGlow));
		//m_pButtons.push_back(new MATUIHelper(m_pPopup->GetWidget("Credits")));
		//MATUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("Music_volume1"));
		//MATUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("Sound_volume1"));

		
	}
}

MATUISetting::~MATUISetting()
{
	for(int i = 0; i < (int)m_pButtons.size(); i++)
	{
		UTIL_SAFE_DELETE(m_pButtons[i]);
	}
	m_pButtons.resize(0);
}

void					MATUISetting::Show()
{
	
	MATUIPopupAnimator::Show();
}

void DELEGATE_CALL		MATUISetting::OnButtonResume(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
    //if(g_pApp && g_pApp->GetRMS())
    //    g_pApp->GetRMS()->Save();
	Hide();
	MATUIAnimator::Instance().Add(m_pAnimateWidget,MATAnimatedUI::ANIMATE_STYLE_HIDE_TORIGHT,0, this);
}

void			MATUISetting::OnAnimatedUIFinish()
{
	if(m_pPopup)
		m_pPopup->Hide(NULL);
}

void	MATUISetting::Tick(float dt)
{
	if(!m_pPopup->IsActive())
		return;

	for(int i = 0; i < (int)m_pButtons.size(); i++)
	{
		m_pButtons[i]->Tick(dt);
	}

	m_AlphaGlow += ( (float)m_GlowAddVal * dt);
	if(m_AlphaGlow > 1.0f)
	{
		m_GlowAddVal = -1;
		m_AlphaGlow = 1.0f;
	}

	if(m_AlphaGlow < 0.0f)
	{
		m_GlowAddVal = 1;
		m_AlphaGlow = 0.0f;
	}

	if(m_pUIButtonGlow)
		m_pUIButtonGlow->SetColor(1.0f,1.0f,1.0f,m_AlphaGlow);
}

bool	MATUISetting::KeyPressed(input::Keyboard::KEY* pKey)
{
	if(m_pPopup->IsActive() && pKey->KeyID == KEY_ESCAPE)
	{
		Hide();
		MATUIAnimator::Instance().Add(m_pAnimateWidget,MATAnimatedUI::ANIMATE_STYLE_HIDE_TORIGHT,0, this);
		return true;
	}
	return false;
}


